package org.edu.page;

import com.github.pagehelper.PageInfo;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class PageUtils
{
    private static final String[] excludeColumns = { "pageIndex", "pageSize", "_search", "nd", "_" };

    public static PageCondition createPageCondition(Map<String, Object> map)
    {
        int pageIndex = 1;
        int pageSize = 8;
        if ((null == map) || (map.isEmpty())) {
            return new PageCondition(pageIndex, pageSize, new HashMap(), null);
        }
        Map<String, Object> filter = (Map)map.get("filter");
        String sorter = MapUtils.getString("sorter", map);
        Integer pageIndexTmp = MapUtils.getInteger("pageIndex", map);
        Integer pageSizeTmp = MapUtils.getInteger("pageSize", map);
        if (null != pageIndexTmp) {
            pageIndex = pageIndexTmp.intValue();
        }
        if (null != pageSizeTmp) {
            pageSize = pageSizeTmp.intValue();
        }
        if (StringUtils.isNotBlank(sorter)) {
            filter.put("sorter", sorter);
        }
        if (null == filter) {
            return new PageCondition(pageIndex, pageSize, new HashMap(), sorter);
        }
        List<String> filterExcludeColumns = Arrays.asList(excludeColumns);
        Map<String, Object> filterTmp = new HashMap();
        for (Map.Entry<String, Object> entry : filter.entrySet()) {
            if (!filterExcludeColumns.contains(entry.getKey())) {
                filterTmp.put(entry.getKey(), entry.getValue());
            }
        }
        return new PageCondition(pageIndex, pageSize, filterTmp, sorter);
    }

    public static PageCondition initPageCondition(PageCondition pageCondition)
    {
        Map<String, Object> filter = pageCondition.getFilter();
        if (null == filter)
        {
            pageCondition.setFilter(new HashMap());
            return pageCondition;
        }
        if (0 == pageCondition.getPageIndex()) {
            pageCondition.setPageIndex(1);
        }
        if (0 == pageCondition.getPageSize()) {
            pageCondition.setPageSize(8);
        }
        List<String> filterExcludeColumns = Arrays.asList(excludeColumns);
        Map<String, Object> filterTmp = new HashMap();
        for (Map.Entry<String, Object> entry : filter.entrySet()) {
            if (!filterExcludeColumns.contains(entry.getKey())) {
                filterTmp.put(entry.getKey(), entry.getValue());
            }
        }
        pageCondition.setFilter(filterTmp);
        return pageCondition;
    }

    public static PageResult getPageReslt(PageCondition pageCondition, List list, Long totalCounts, int totalPages)
    {
        PageResult pageResult = new PageResult();
        pageResult.setItems(list);
        pageResult.setCurrPage(pageCondition.getPageIndex());
        pageResult.setPageSize(pageCondition.getPageSize());
        pageResult.setTotalCount(totalCounts.longValue());
        pageResult.setTotalPage(totalPages);
        return pageResult;
    }

    public static <T> PageResult getPageReslt(PageInfo<T> pageInfo)
    {
        PageResult pageResult = new PageResult();
        pageResult.setCurrPage(pageInfo.getPageNum());
        pageResult.setItems(pageInfo.getList());
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setTotalCount(pageInfo.getTotal());
        pageResult.setTotalPage(pageInfo.getPages());
        return pageResult;
    }

    public static <T> PageResult getPageReslt(PageInfo<T> pageInfo, List<?> items)
    {
        PageResult pageResult = getPageReslt(pageInfo);
        pageResult.setItems(items);
        return pageResult;
    }

    public static PageResult getAllPageReslt(List list)
    {
        PageResult pageResult = new PageResult();
        pageResult.setItems(list);
        pageResult.setCurrPage(1);
        pageResult.setPageSize(0);
        pageResult.setTotalCount(list.size());
        pageResult.setTotalPage(1L);
        return pageResult;
    }
}
